(function() {
	Path.on("/user/__basename__.html", function(_current_location) {
		App.set('title', '商品评价');
		var queryString = _current_location.query;
		var oid = queryString.get("id");
		if (!oid) {
			alert("error", "无效的订单编号");
			Path.jump("/user/order.html");
		};
		// 商品详情
		coAjax.get(appConfig.user.ach_by_id_with_eval + oid, function(result) {
			var ach = result.result;
			jSouper.forEach(ach.info.goods_info, function(goods_info) {
				//初始化评价等级为10
				goods_info._eval_star_num = 10
			});
			App.set("$Cache.user.comment.appraise", ach);
		}, function(errorCode) {
			console.log("errorCode:", errorCode);
			alert("error", "无效的订单编号");
		});
		// 检查评价等级
	});
	App.set('$Event.user.comment.eval.checkStar', function(e, vm) {
		var star_num = parseInt(this.value);
		if (star_num > 10) {
			vm.set('_eval_star_num', 10);
		} else if (star_num < 0) {
			vm.set('_eval_star_num', 0);
		} else if (isNaN(star_num)) {
			vm.set('_eval_star_num', 10);
		} else {
			vm.set('_eval_star_num', star_num);
		};
	});
	// 提交评价
	App.set("$Event.user.comment.eval.submitEval", function(e, vm) {
		var evaluationData = [];
		jSouper.forEach(App.get("$Cache.user.comment.appraise.info.goods_info"), function(goods_info) {
			evaluationData.push({
				goods_id: goods_info.goods._id,
				star_num: ~~goods_info._eval_star_num || 10,
				isAny: !!goods_info._eval_isAny,
				content: goods_info._eval_content
			});
		});
		var url;
		var state = App.get("$Cache.user.comment.appraise.info.state");
		if (state == appConfig.ACH_STATE["ol交易完成"]) {
			url = appConfig.user["ach_ol交易完成_to_ol已评价"]
		} else if (state == appConfig.ACH_STATE["交易完成"]) {
			url = appConfig.user["ach_交易完成_to_已评价"]
		} else {
			alert("error", "错误的订单状态")
			return;
		}
		coAjax.put(url + App.get("$Cache.user.comment.appraise._id"), {
			evaluations: evaluationData
		}, function(result) {
			alert("评价完成");
			console.log(result.result);
			App.set("$Cache.user.comment.appraise.info.state", result.result);
			alert("success", "完成评价！！！");
			Path.jump("/user/order.html");
		});
	});
}());